using System;
using System.Collections;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Expression;
using Saffron.Framework.Users;

namespace Saffron.Framework.Projects
{
    [ActiveRecord]
    public class Project : ActiveRecordBase
    {
        private int _id;
        private string _name;
        private string _description;
        private UserAccount _createdBy;
        private DateTime _createdDate;
        private ApplicationProcess _applicationProcess = ApplicationProcess.InviteOnly;
        private ProjectVisibility _visibility = ProjectVisibility.Private;
        private IList _members = new List<UserAccount>();
        [PrimaryKey]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property(NotNull=true,Unique=true)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Property]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        [Property(NotNull=true)]
        public ApplicationProcess ApplicationProcess
        {
            get { return _applicationProcess; }
            set { _applicationProcess = value; }
        }

        [Property(NotNull=true)]
        public ProjectVisibility Visibility
        {
            get { return _visibility; }
            set { _visibility = value; }
        }


        [BelongsTo("CreatedBy", NotNull = true)]
        public UserAccount CreatedBy
        {
            get { return _createdBy; }
            set { _createdBy = value; }
        }

        [Property]
        public DateTime CreatedDate
        {
            get { return _createdDate; }
            set { _createdDate = value; }
        }

        [HasAndBelongsToMany(typeof(UserAccount), Table = "ProjectMembers", ColumnKey = "UserAccountId", ColumnRef = "ProjectId")]
        public IList Members
        {
            get { return _members; }
            set { _members = value; }
        }



        public static Project FindByPrimaryKey(int id)
        {
            return (Project)FindByPrimaryKey(typeof(Project), id);
        }

        public static Project[] FindAllByCreatedBy(UserAccount user)
        {
            return (Project[])FindAllByProperty(typeof(Project), "CreatedBy", user);
        }

        public static Project[] FindAll()
        {
            return (Project[])FindAll(typeof(Project));
        }

        // need to add the creator to the member list
        public override void Create()
        {
            Members.Add(CreatedBy);  
            base.Create();
        }

        public bool UserCanViewProject(UserAccount user)
        {
            //Anyone can view the project if it's public
            if (Visibility == ProjectVisibility.Public)
                return true;

            //Check for membership in project
            //TODO:

            //User can always see it if they created it
            if (user.Id == CreatedBy.Id)
                return true;

            //Admins can see all projects
            if (user.IsInRole(SystemRoleType.Admin.ToString()))
                return true;

            return false;
        }
        
    }
}
